#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Created on 18-1-12
@author: yzw
@desc:
"""
import json
import os

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt


# 博客图片上传
@csrf_exempt
def upload_img(request):
	if request.method == 'POST':
		blog_img = request.FILES['blog_img']
		if not blog_img:
			return HttpResponse('{"msg": "no files for upload!"}', content_type="application/json")
		file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../static/img"))
		destination = open(os.path.join(file_path, blog_img.name), 'wb+')  # 打开特定的文件进行二进制的写操作
		for chunk in blog_img.chunks():  # 分块写入文件
			destination.write(chunk)
		destination.close()
		result_dict = {"path": '../static/img/' + blog_img.name, 'msg': '上传完成'}
		return HttpResponse(json.dumps(result_dict), content_type="application/json")
